// 导入接口api
import { findAllCategory } from '@/api/category.js'
// 导入常量，用于请求失败的默认渲染
import { topCategory } from '@/api/constants'

export default {

  namespaced: true,
  state: () => {
    return {
      list: topCategory.map(item => {
        return { name: item }
      })
    }
  },

  mutations: {
    setList (state, cateList) {
      state.list = cateList
    },

    // 提供修改open属性的方法

    show (state, item) {
      // 引用数据类型
      const category = state.list.find(category => category.id === item.id)

      category.open = true
    },

    hide (state, item) {
      // 引用数据类型
      const category = state.list.find(category => category.id === item.id)

      category.open = false
    }
  },

  actions: {
    async getList ({ commit }) {
      const { result } = await findAllCategory()

      // 将返回结果进行处理，都设置一个open属性，用于控制其显示或者隐藏
      result.forEach((item) => {
        item.open = false
      })

      console.log(result)
      commit('setList', result)
    }
  }
}
